<template>
  <div class="firstPurchase">
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>会员充值</el-breadcrumb-item>
        <el-breadcrumb-item>弹窗配置后台</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <!-- <el-tag>{{ scope.row.edit_admin_name }}</el-tag> -->
        </template>
        <!-- 自定义操作栏 -->
        <template slot-scope="{row,index}" slot="menu">
          <div v-if="row.is_online == 0">
            <el-button
              icon="el-icon-primary"
              type="text"
              size="small"
              @click.stop="updateFlagFn(row)"
            >上线</el-button>
            <span> \ </span>
            <el-button
              icon="el-icon-edit"
              type="text"
              size="small"
              @click="$refs.crud.rowEdit(row,index)"
            >编辑</el-button>
          </div>
          <div v-else>
            <el-button
              icon="el-icon-primary"
              type="text"
              size="small"
              @click.stop="updateFlagFn(row)"
            >下线</el-button>
            <span> \ </span>
            <el-button
              icon="el-icon-view"
              type="text"
              size="small"
              @click="$refs.crud.rowView(row,index)"
            >查看</el-button>
          </div>
        </template>
        <!-- 弹窗标题-配置多语言 -->
        <template slot-scope="scope" slot="pop_title">
          <div>
            <span>{{ scope.row.pop_title }}</span>
            <el-button
              icon="el-icon-circle-plus"
              type="text"
              size="small"
              @click.stop="getMultilingualDetail(scope.row.pop_title)"
              v-if="
                scope.row.pop_title != undefined && scope.row.pop_title.trim().length != 0
              "
            >
              <font>配置多语言</font>
            </el-button>
          </div>
        </template>
        <template slot-scope="scope" slot="set_start_timeForm">
          <div style="white-space: nowrap;">
            <el-input v-model="scope.row.set_start_time"></el-input>
            <span> 小时后启动弹窗</span>
          </div>
        </template>
        <template slot-scope="scope" slot="set_end_timeForm">
          <div style="white-space: nowrap;">
            <el-input v-model="scope.row.set_end_time"></el-input>
            <span> 天关闭</span>
          </div>
        </template>
      </avue-crud>
    </basic-container>
    <!-- 弹出框 -->
    <el-dialog
      title="多语言配置"
      :visible.sync="multilingualDialogVisible"
      :append-to-body="true"
    >
      <el-form :model="multilingual_data" label-width="120px">
        <!-- <el-form-item label="文本说明：">
          <el-descriptions-item>{{
            multilingual_data.txt
          }}</el-descriptions-item>
        </el-form-item> -->

        <el-form-item label="英语文本：" prop="bookIntro">
          <el-input
            type="textarea"
            v-model="multilingual_data.en_txt"
            placeholder="请输入英语文本"
          ></el-input>
        </el-form-item>

        <el-form-item label="芬兰语文本：" prop="bookIntro">
          <el-input
            type="textarea"
            v-model="multilingual_data.fn_txt"
            placeholder="请输入芬兰语文本"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="multilingualDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateMultilingual">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  appConfigPopWindow as baseURL,
  selectVersion,
  appConfigDefaultTxt,
} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      multilingualDialogVisible: false,
      multilingual_data: {
        txt: "",
        en_txt: "",
        fn_txt: "",
      },
      params: {},
      form: {},
      default: {
        orderByList: "sort&ASC,create_time&DESC",
      },
      option: {
        addBtn: true,
        editBtn: false,
        delBtn: true,
        searchBtn: true,
        refreshBtn: true,
        excelBtn: false,
        labelWidth: 120,
        column: [
          {
            label: "版本号",
            prop: "app_version",
            type: "select",
            search: true,
            span: 12,
            placeholder: "请输入版本号",
            dicUrl: `${catUrl}/${selectVersion.selectVersion}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "用户端",
            prop: "user_port",
            type: "select",
            search: true,
            dicData: [
              {
                label: "Android",
                value: 0,
              },
              {
                label: "IOS",
                value: 1,
              },
              {
                label: "通用",
                value: 2,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "弹窗标题",
            prop: "pop_title",
            type: "input",
            // align: "center",
            search: true,
            slot: true,
            placeholder: "请输入弹窗标题",
            rules: [
              {
                required: true,
                message: "弹窗标题必须填写",
              },
            ],
          },
          {
            label: "充值金额(元)",
            prop: "price",
            type: "input",
            controls:false,
            search: false,
            placeholder: "请输入充值金额",
            rules: [
              {
                required: true,
                message: "充值金额必须填写",
              },
            ],
          },
          {
            label: "金币",
            prop: "coin",
            type: "input",
            controls:false,
            search: false,
            placeholder: "请输入金币",
            rules: [
              {
                required: true,
                message: "金币必须填写",
              },
            ],
          },
          {
            label: "赠币",
            prop: "give_coin",
            type: "input",
            controls:false,
            search: false,
            placeholder: "请输入赠币",
            rules: [
              {
                required: true,
                message: "赠币必须填写",
              },
            ],
          },
          {
            label: "用户注册时间",
            prop: "set_start_time",
            formslot: true,
            row: true,
            search: false,
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入时间",
              }
            ]
          },
          {
            label: "弹窗生效后",
            prop: "set_end_time",
            formslot: true,
            row: true,
            search: false,
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入时间",
              }
            ]
          },
          {
            label: "google商品id",
            prop: "google_product_id",
            type: "input",
            align: "center",
            search: false,
            hide: true,
            display: true,
          },
          {
            label: "苹果商品id",
            prop: "apple_product_id",
            type: "input",
            search: false,
            hide: true,
            display: true,
          },
          {
            label: "最后操作信息",
            prop: "edit_time",
            type: "input",
            // slot: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  watch: {
    "form.user_port"(val) {
      if (val === 0) {
        this.findObject(
          this.option.column,
          "google_product_id"
        ).display = true;
        this.findObject(
          this.option.column,
          "apple_product_id"
        ).display = false;
      } else if (val === 1) {
        this.findObject(
          this.option.column,
          "google_product_id"
        ).display = false;
        this.findObject(
          this.option.column,
          "apple_product_id"
        ).display = true;
      } else {
        this.findObject(
          this.option.column,
          "google_product_id"
        ).display = true;
        this.findObject(
          this.option.column,
          "apple_product_id"
        ).display = true;
      }
    },
  },
  methods: {
    async updateFlagFn(row) {
      row.is_online = row.is_online == 0 ? 1 : 0;
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      let data = await common(baseURL.updatePopWindow, row, null, "POST");
      if (data.code == 1) {
        return;
      } else {
        this.getList();
      }
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.getPopWindowList,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      // this.data = response.data.map(item=>{
      //   item.user_port = item.user_port + ''
      //   return item
      // })
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      row.user_port = row.user_port === "" ? 2 : row.user_port;
      row.is_online = 1;
      row.pop_type = 1;
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      // this.convert(row);
      let data = await common(baseURL.savePopWindow, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**编辑 */
    rowEdit(row) {
      this.$refs.crud.rowEdit(row, row.$index);
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      row.user_port = row.user_port === "" ? 2 : row.user_port;
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      let data = await common(baseURL.updatePopWindow, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          row.is_delete = 1;
          let userInfo = this.$store.state.user.userInfo;
          row.edit_admin_name = userInfo.username;
          row.edit_admin_id = userInfo.id;
          let data = await common(baseURL.updatePopWindow, row, null, "POST");
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    /**获取多语言配置 */
    async getMultilingualDetail(txt) {
      let multilingual_res = await common(
        `${appConfigDefaultTxt.selectAppConfigDefaultTxt}?txt=${txt}&showDetail=true`,
        null,
        null,
        "GET"
      );
      if (
        multilingual_res.data == undefined ||
        multilingual_res.data.id == undefined
      ) {
        this.multilingual_data = {
          txt: txt,
          en_txt: "",
          fn_txt: "",
          type: "add",
          // updateIndex: updateIndex,
        };
      } else {
        this.multilingual_data = multilingual_res.data;
        this.multilingual_data.type = "edit";
        // this.multilingual_data.updateIndex = updateIndex;
      }
      this.multilingualDialogVisible = true;
    },
    /**修改多语言配置 */
    async updateMultilingual() {
      let userInfo = this.$store.state.user.userInfo;
      let edit_admin_id = userInfo.id;
      let edit_admin_name = userInfo.username;
      let update;
      if (this.multilingual_data.type == "add") {
        update = await common(
          appConfigDefaultTxt.save,
          { ...this.multilingual_data, edit_admin_id, edit_admin_name },
          null,
          "POST"
        );
      } else {
        update = await common(
          appConfigDefaultTxt.update,
          { ...this.multilingual_data, edit_admin_id, edit_admin_name },
          null,
          "POST"
        );
      }
      if (update.code == 1) {
        this.multilingualDialogVisible = false;
      }
    },
  },
};
</script>
<style scope>
.firstPurchase .el-table .el-table__row td:last-child .cell{
  display: flex;
  flex-direction: row-reverse;
  justify-content: space-around;
}
</style>